#include <linux/module.h>
#include <linux/init.h>
#include <linux/types.h>
#include <linux/sched.h>
#include <linux/slab.h>
#include <asm/desc_defs.h>
#include "load_handler.h"
#include <linux/cred.h>
#include <linux/uidgid.h>

unsigned long get_idt_addr(void);
void set_new_idt(void);
void set_handler(int,unsigned long);
unsigned long get_handler(int);
void my_handler(void);
void stub(void);

int init(void){
	int i = 3;
	unsigned long new_addr;

	idt_addr = get_idt_addr();
	if( idt_addr < PAGE_OFFSET) return 0;
	printk( KERN_ALERT "IDT address - 0x%08x\n", (unsigned int)(idtr.address));	
	set_new_idt();
	printk(KERN_ALERT "NEW IDT address - 0x%08x\n", (unsigned int)(idtr.address));

	new_handler = (unsigned long)&my_handler;
	old_handler = get_handler(i);
	if( old_handler < PAGE_OFFSET) return 0;
	printk( KERN_ALERT "Old handler - 0x%08x\n", (unsigned int)old_handler);
	new_addr = (unsigned long)&stub;
	set_handler(i, new_addr);
	printk(KERN_ALERT "New handler - 0x%08x\n", (unsigned int)new_addr);
	return 0;
}

void cleanup(void){
	unsigned long flags;
	
	idtr.address = (unsigned long)idt_addr;
	save_flags(flags);
	cli();
	asm("lidt %0"::"m" (idtr));
	restore_flags(flags);
	
	kfree(idt);
	printk(KERN_ALERT "Old IDT address restore (0x%08x)\n", (unsigned int)(idtr.address));
	}

module_init(init);
module_exit(cleanup);


	
